<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Admin\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    //
    /**
     * 修改密码页面
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function changePassword()
    {
        $user = session('user');
        if (empty($user)){
            redirect('admin/index');
        }
        $User = User::findOrFail($user['id']);
        if (empty($User)){
            redirect('admin/index');
        }
        return view('admin.user.change_password');
    }

    /**
     * 执行修改密码
     *
     * @param Request $request
     * @return array
     */
    public function doChangePassword(Request $request)
    {
        $user = User::findOrFail(session('user')['id']);

        if (!password_verify($request->get('password'),$user->password)){
            return $this->res_msg(1,'密码不正确');
        }
        if ($request->get('new_password') !== $request->get('check_new_password')){
            return $this->res_msg(2,'新密码不一致');
        }

        $user->password = password_hash($request->get('new_password'),PASSWORD_DEFAULT);
        if ($user->save()){
            session()->forget('user');
            return $this->res_msg(0,'修改成功');
        }
    }


}
